// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import ElementUI from 'element-ui';
import {Loading} from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import App from './App'
import router from './router'

//导入iconfont图标
import './assets/iconfont/iconfont.css'



//PDF预览地址前缀
axios.default.baseUrl="";
axios.default.pdfViewerURL="http://scp.metahike.com/pdfviewer/web";


//导入axios
import axios from 'axios'
import VueAxios from 'vue-axios'
Vue.use(VueAxios, axios);
require('@/utils/request.js')

//引用el-dialog拖动逻辑
import vueDialogDrag from '@/utils/directive' // 拖拽
vueDialogDrag.install(Vue)


Vue.use(ElementUI,{size:"mini"});

//上传用axios设置
const axiosUploader=axios.create({
  baseURL:"",
  headers:{"Content-Type":"multipart/form-data"},
})

axiosUploader.interceptors.request.use(config => {
  //Header中携带其他信息
  if (localStorage.getItem("token") != null) {
    config.headers['token'] = localStorage.getItem("token");
  }
  //console.log("上传token",localStorage.getItem("token"));
  //使用 qs转换后，请求数据将变为 formData形式,否则是 Playload形式
  //config.data = qs.stringify(config.data);
  //console.error("发送请求");
  return config;
}, error => {
  //console.error("发送请求出错",error);
  return Promise.reject(error);
})

axiosUploader.interceptors.response.use(
  response => {
    if (response.status == 200) {
      if(response.data.code==401){
        ElementUI.Message({message:"登录超时或无权访问,请重新登录!",type:"error"});
        localStorage.removeItem("userinfo");
        localStorage.removeItem("token");

        router.push("/login");
      }
      return response;
    }
  }, error => {
    console.log(error);
    if(error && error.response){
       switch(error.response.status){
         case 504:
         ElementUI.Message({message:"服务器无响应!",type:"error"});
         break;
        case 404:
         ElementUI.Message({message:"服务器不存在此接口!"+error,type:"error"});
        break;
       }
     }
    return Promise.reject(error);
  }
)


Vue.prototype.$axiosUploader=axiosUploader;

Vue.config.productionTip = false

Vue.prototype.DNumber=function(value, len) {
  var f = parseFloat(value);
  var f = Math.round(value * 10000) / 10000;
  var s = f.toString();
  var rs = s.indexOf('.');
  if (rs < 0) {
    rs = s.length;
    s += '.';
  }
  while (s.length <= rs + len) {
    s += '0';
  }
  return s;
}

Vue.prototype.CurrentDateTime=function(){
   let now = new Date(),
      y = now.getFullYear(),
      m = now.getMonth() + 1,
      d = now.getDate();
    return y +"-"+(m < 10 ? "0" + m : m) +"-"+(d < 10 ? "0" + d : d);// + " " + now.toTimeString().substr(0, 8);
 }


Vue.prototype.CopyText=function(text){
    var textarea = document.createElement('textarea');
    textarea.style.position = 'fixed';
    textarea.style.opacity = 0;
    textarea.value = text;
    document.body.appendChild(textarea);
    textarea.select();
    document.execCommand('copy');
    document.body.removeChild(textarea);
}

import echarts from 'echarts'
Vue.prototype.$echarts = echarts;


//文件下载函数
Vue.prototype.downloadFile =function(url, data)  {
    return axios({
        url,
        method:'POST',
        data,
        responseType: 'blob',
        headers: {
            'X-Token': localStorage.getItem('token') || '',
        },
    })
        .then(res => {
            const BLOB = res.data;
                    const fileReader = new FileReader();
                    fileReader.readAsDataURL(BLOB,{type:"application/vnd.ms-excel"});  //对请求返回的文件进行处理
                    fileReader.onload = (e) => {
                        let a = document.createElement('a');
                        a.download = data.name + '.xlsx'
                        a.href = e.target.result;
                        document.body.appendChild(a)
                        a.click();
                        document.body.removeChild(a)
                    }

                console.log("下载成功!");

        })
        .catch(err => {
            console.log(err || '下载失败！')
        })
}





/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
